package com.cssw.fyzb.core.dao.system;


import com.cssw.fyzb.core.model.dodb.SysFunc;
import com.cssw.fyzb.core.util.StringUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2020/10/10 15:10
 */
@Repository
public class FuncDaoImpl implements FuncDao {

    /**
     * 查询菜单列表
     *
     * @param page
     * @param limit
     * @param param
     * @return
     */
    public Page<SysFunc> list(int page, int limit, Map param) {
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("select * ");
        sqlParse.addSQL(" from sys_func where state='1'");
        sqlParse.addLikeSQL(" and func_name like :funcName");
        sqlParse.addLikeSQL(" and func_code like :funcCode");
        sqlParse.addLikeSQL(" and func_url like :funcUrl");
        sqlParse.addSQL(" and status=:status");
        sqlParse.addSQL(" and func_type=:funcType");

        sqlParse.addSQL(" order by func_id desc ");
        return SysFunc.dao.paginate(page, limit, sqlParse);
    }

    /**
     * 查询所有有效的菜单列表
     *
     * @return
     */
    public List<SysFunc> listAllValid() {
        StringBuffer sql = new StringBuffer();
        sql.append("select * ")
                .append(" from sys_func where state='1'")
                .append(" and status=?")
                .append(" order by sort desc ");
        return SysFunc.dao.find(sql.toString(), SysFunc.STATUS_START);
    }

    /**
     * 查询所有有效的菜单列表
     *
     * @return
     */
    public List<SysFunc> listAll() {
        StringBuffer sql = new StringBuffer();
        sql.append("select * ")
                .append(" from sys_func ")
                .append(" where state = '1' ")
                .append(" order by sort desc ");
        return SysFunc.dao.find(sql.toString());
    }


    /**
     * 新增菜单
     *
     * @param supFuncId
     * @param funcName
     * @param funcCode
     * @param funcType
     * @param funcUrl
     * @param status
     * @return
     */
    public Long save(Long supFuncId, String funcName, String funcCode,
                     String funcIcon, String funcType, String funcUrl, String status, int sort,Long sysOperId) {
        SysFunc func = new SysFunc();
        func.setFuncName(funcName);
        func.setSupFuncId(supFuncId);
        func.setFuncCode(funcCode);
        func.setFuncType(funcType);
        func.setFuncUrl(funcUrl);
        if (!StringUtil.isEmpty(funcIcon)) {
            func.setFuncIcon(funcIcon);
        }
        func.setStatus(status);
        func.setState(SysFunc.STATE_NORMAL);
        func.setSort(sort);
        //func.setCreateOperId(10000l);
        func.setCreateOperId(sysOperId);
        func.setCreateTime(new Date());
        func.save();
        return func.getFuncId();
    }

    /**
     * 修改菜单
     *
     * @param funcId
     * @param supFuncId
     * @param funcName
     * @param funcCode
     * @param funcType
     * @param funcUrl
     * @param status
     */
    public void update(Long funcId, Long supFuncId, String funcName, String funcCode,
                       String funcIcon, String funcType, String funcUrl, String status, int sort,Long sysOperId) {
        SysFunc func = new SysFunc();
        func.setFuncId(funcId);
        func.setSupFuncId(supFuncId);
        func.setFuncName(funcName);
        func.setFuncCode(funcCode);
        if (!StringUtil.isEmpty(funcIcon)) {
            func.setFuncIcon(funcIcon);
        }
        func.setFuncType(funcType);
        func.setFuncUrl(funcUrl);
        func.setStatus(status);
        func.setSort(sort);
        func.setUpdateOperId(sysOperId);
        func.setUpdateTime(new Date());
        func.update();
    }

    /**
     * 删除菜单
     *
     * @param funcId
     */
    public void delete(Long funcId) {
        SysFunc.dao.deleteById(funcId);
    }


    /**
     * 根据菜单编码查询菜单信息
     *
     * @param funcCode
     */
    public SysFunc getByCode(String funcCode) {
        String sql = "select * from sys_func where func_code = ? and state = '1'";
        return SysFunc.dao.findFirst(sql, funcCode);
    }

    /**
     * 根据菜单标识查询菜单信息
     *
     * @param funcId
     */
    public SysFunc getById(Long funcId) {
        return SysFunc.dao.findById(funcId);
    }

    /**
     * 修改菜单状态
     *
     * @param funcId
     */
    public void updateStatus(Long funcId, String status,Long userId) {
        SysFunc func = new SysFunc();
        func.setFuncId(funcId);
        func.setStatus(status);
        func.setUpdateOperId(userId);
        func.setUpdateTime(new Date());
        func.update();
    }

    /**
     * 查询菜单列表
     *
     * @param operId
     * @return
     */
    public List<SysFunc> listMenuByOperId(Long operId) {
        StringBuffer sql = new StringBuffer();
        sql.append("select distinct t1.func_id, t1.sup_func_id, t1.func_name, t1.func_type, ")
                .append(" t1.func_code, t1.func_url, t1.func_icon, t1.sort, t1.status, t1.state, t1.create_oper_id, ")
                .append(" t1.create_time ")
                .append(" from sys_func t1, sys_role_func t2, sys_oper_role t3 ")
                .append(" where t3.oper_id = ? and t2.role_id = t3.role_id ")
                .append(" and t1.func_id = t2.func_id and t1.status = ?")
                .append(" and t1.func_type in ('1','2') and t1.state = '1'");
        return SysFunc.dao.find(sql.toString(), operId, SysFunc.STATUS_START);
    }

    /**
     * 查询按钮列表
     *
     * @param operId
     * @return
     */
    public List<SysFunc> listFuncByOperId(Long operId) {
        StringBuffer sql = new StringBuffer();
        sql.append("select distinct t1.func_id, t1.sup_func_id, t1.func_name, t1.func_type, ")
                .append(" t1.func_code, t1.func_url, t1.func_icon, t1.sort, t1.status, t1.state, t1.create_oper_id, ")
                .append(" t1.create_time ")
                .append(" from sys_func t1, sys_role_func t2, sys_oper_role t3 ")
                .append(" where t3.oper_id = ? and t2.role_id = t3.role_id ")
                .append(" and t1.func_id = t2.func_id and t1.status = ?")
                .append(" and t1.func_type = '3' and t1.state='1' ");
        return SysFunc.dao.find(sql.toString(), operId, SysFunc.STATUS_START);
    }

    /**
     * 获取所有父节点
     *
     * @param funcId
     * @return
     */
    public String getSupFuncIds(Long funcId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT GROUP_CONCAT(T2.func_id) ")
                .append(" FROM (")
                .append("        SELECT").append(" FROM (")
                .append(" @r AS _id,")
                .append(" (SELECT @r := p_func_id FROM sys_func WHERE func_id = _id) AS p_func_id, ")
                .append(" @l := @l + 1 AS lvl ")
                .append("  FROM ")
                .append("        (SELECT @r := ?, @l := 0) vars,")
                .append("        sys_func h")
                .append(" WHERE @r <> 0) T1 ")
                .append(" JOIN sys_func T2 ")
                .append(" ON T1._id = T2.func_id ")
                .append(" where t2.state = '1'")
                .append(" ORDER BY T1._id asc");
        return Db.queryStr(sql.toString(), funcId);
    }
}
